#!/usr/bin/env Rscript

library(tidyverse)

complete <- read_csv2("../app/data/completo.csv", na = "NA",
                      col_types = cols(
                          pais = col_character(),
                          regiao = col_character(),
                          uf = col_character(),
                          mesorregiao = col_character(),
                          microrregiao = col_character(),
                          municipio = col_character(),
                          st_acidente_feriado = col_character(),
                          ds_agente_causador = col_character(),
                          ano_cat = col_integer(),
                          ds_cnae_classe_cat = col_character(),
                          dt_acidente = col_date(),
                          st_dia_semana_acidente = col_character(),
                          ds_emitente_cat = col_character(),
                          hora_acidente = col_time(),
                          idade_cat = col_integer(),
                          cd_indica_obito = col_character(),
                          ds_natureza_lesao = col_character(),
                          ds_cbo = col_character(),
                          ds_parte_corpo_atingida = col_character(),
                          cd_tipo_sexo_empregado_cat = col_character(),
                          ds_tipo_acidente = col_character(),
                          ds_tipo_local_acidente = col_character()
                      ))

estimativa_pop <- read_csv2("../app/data/estimativas.csv", na = "NA",
                            col_types = cols(
                                uf = col_character(),
                                municipio = col_character(),
                                populacao = col_integer(),
                                ano = col_integer()
                            ))
estimativa_pop <- rename(estimativa_pop, ano_cat = ano)

#Summarization of the number of accidents occurred by year 2012-2016
ac_mun_2012 <- complete %>%
    group_by(uf, municipio, ano_cat) %>%
    filter(ano_cat == 2012) %>%
    summarize(acidentes = n())

ac_mun_2013 <- complete %>%
    group_by(uf, municipio, ano_cat) %>%
    filter(ano_cat == 2013) %>%
    summarize(acidentes = n())

ac_mun_2014 <- complete %>%
    group_by(uf, municipio, ano_cat) %>%
    filter(ano_cat == 2014) %>%
    summarize(acidentes = n())

ac_mun_2015 <- complete %>%
    group_by(uf, municipio, ano_cat) %>%
    filter(ano_cat == 2015) %>%
    summarize(acidentes = n())

ac_mun_2016 <- complete %>%
    group_by(uf, municipio, ano_cat) %>%
    filter(ano_cat == 2016) %>%
    summarize(acidentes = n())

ac_mun <- rbind(ac_mun_2012, ac_mun_2013, ac_mun_2014, ac_mun_2015, ac_mun_2016)
est_mun <- estimativa_pop %>% filter(ano_cat < 2017)

write_delim(ac_mun_2012, "../app/data/ac_mun_2012.csv", delim = ";")
write_delim(ac_mun_2013, "../app/data/ac_mun_2013.csv", delim = ";")
write_delim(ac_mun_2014, "../app/data/ac_mun_2014.csv", delim = ";")
write_delim(ac_mun_2015, "../app/data/ac_mun_2015.csv", delim = ";")
write_delim(ac_mun_2016, "../app/data/ac_mun_2016.csv", delim = ";")

